<template>
	<view>
		<view class="mycontent">
			<input type='text' style="margin: 20rpx;" v-model="ssid" placeholder="请输入wifi的SSID" disabled=true><br>
			<input type='text' style="margin: 20rpx;" v-model="psw" placeholder="请输入wifi的密码"><br>
			<button type="primary" style="margin: 20rpx;" @click="doSmartConfig">一键配网</button>
		</view>
<!-- 		<view>
			功能已被禁用，即将返回上一页...
		</view> -->
	</view>

	<!-- <view class="button-sp-area">
		<button type="primary" plain="true" @click="showRichAlert()">RichAlert测试</button>
		<button type="primary" plain="true" @click="getWifiInfo()">getWifiInfo</button>
		<button type="primary" plain="true" @click="smartConfig()">smartConfig</button>
		<button type="primary" plain="true" @click="cancel()">cancel smartConfig</button>
	</view> -->
</template>

<script>
	// 获取 module
	var smartConfigModule = uni.requireNativePlugin("SmartConfigModule")
	// var backPopupModule = uni.requireNativePlugin("BackPopupModule")
	const modal = uni.requireNativePlugin('modal');
	import { listDevice, listDelDevice, updateDevice } from "@/api/system/device.js";
	import msgUtils from "@/plugins/modal.js";
	
	export default {
		data() {
			return {
				ssid:"",
				psw:"",
				deviceType:-1,
				queryParamsDevice: {
				  pageNum: 1,
				  pageSize: 1,
				  productionId: -1,
				},
				newDeviceName:"",
				newDeviceType:-1
			}
		},
		onLoad: function (option) {
			// disabled smartconfig 20240721 by bob
			// enabled smartconfig 20241225 by bob
			// setTimeout(()=>{
			// 	uni.navigateBack();
			// }, 1000)
			
			console.log(option.type);
			let that = this;
			that.deviceType = option.type;
			//监听android原生端调用的前端方法：wifiChange，用于修改ssid（当wifi切换时自动会触发）
			
			
			plus.globalEvent.addEventListener('wifiChange', function(data){
				that.ssid = data.ssid;//当wifi切换时，修改ssid文本框
			});
			
			
			this.initSmartConfig();
			// backPopupModule.startTelephoneService();
			
		},
		methods: {
			// disabled smartconfig 20240721 by bob
			// enabled smartconfig 20241225 by bob
			initSmartConfig(){
				smartConfigModule.initSmartConfig((ret) =>{
					if(ret.code!=200){
						modal.toast({
							message: "SmartConfig初始化失败",
							duration: 3
						});
					}else{
						modal.toast({
							message: "SmartConfig初始化成功",
							duration: 3
						});
					}
				});
			},
			initDevice(){
				console.log("123456res");
				listDevice(this.queryParamsDevice).then(res=>{
					this.newDeviceName = res.rows[0].name;
					this.newDeviceType = res.rows[0].type;
					console.log(res);
				});
				listDelDevice(this.queryParamsDevice).then(res=>{
					this.newDeviceName = res.rows[0].name;
					this.newDeviceType = res.rows[0].type;
					updateDevice({"delFlag":0, "name":this.newDeviceName}).then(res0=>{
						console.log(res0);
					})
				});
			},
			doSmartConfig(){
				if(this.psw!=""){
					uni.showLoading({
						'title':'开始配网……'
					});
					smartConfigModule.doSmartConfig({
						'psw':this.psw,
						'deviceCount':1
					},(ret)=>{
						let queryString = `code=${ret.code}`;
						console.log(ret);
						if(ret.code==200){
							modal.toast({
								message: ret.ip,
								duration: 3
							});
							
							setTimeout(()=> {
								const that = this;
								var count = 0;
								uni.switchTab({
									url:`/pages/devices?${queryString}`,
									success:(res) => {
									}
								})
								let intervalId = setInterval(()=>{
									that.initDevice();
									count += 1;
									let myId = intervalId;
									if(that.newDeviceName!='' && that.newDeviceType==that.deviceType){
										clearInterval(myId);
										uni.hideLoading();
										msgUtils.msgSuccess('已添加新设备！设备名称：' + that.newDeviceName)
										count=12;
									}
									else if(count==12){
										clearInterval(myId);
										uni.hideLoading();
										msgUtils.msgError('添加新设备超时，请确认！')
									}
								},2500);
							}, 1000);
						}else{
							modal.toast({
								message: ret.msg,
								duration: 3
							});
							uni.hideLoading();
						}
					});
				} else {
					uni.showModal({
						content:"WIFI密码不能为空"
					})
				}
			}
		},
	}
</script>
